# import re
import json
import requests
from bs4 import BeautifulSoup


class Weather_data:
    weather_infos = {"城市": [], "现在": [], "今天": [], "明天": [], "后天": [], "大后天": []}

    def __init__(self):
        self.header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
        }

    def get_data(self, pro, city, country=None):
        Weather_data.weather_infos = {"城市": [], "现在": [], "今天": [], "明天": [], "后天": [], "大后天": []}
        if country is None:
            self.url = f'https://www.msn.cn/zh-cn/weather/forecast/in-{pro},{city}'
        else:
            self.url = f'https://www.msn.cn/zh-cn/weather/forecast/in-{pro},{city},{country}'

        r = requests.get(self.url, headers=self.header)
        # 数据清洗

        self.soup = BeautifulSoup(r.text, 'html.parser')
        html_data = self.soup.findAll('script', type='application/json')
        for data in html_data:
            json_data = json.loads(data.string)
            current_weather = json_data["entryPoint"]["WeatherPageV2_default__WeatherPageV2"]["_@STATE@_"][
                "currentWeatherCondition"]
            other_day = json_data["WeatherData"]
            Weather_data.weather_infos["城市"] = other_day["_@STATE@_"]["currentLocation"]["displayName"]
            Weather_data.weather_infos["现在"].append(current_weather["shortCap"])
            Weather_data.weather_infos["现在"].append(current_weather["currentTemperature"])
            Weather_data.weather_infos["现在"].append(current_weather["aqiSeverity"])
            Weather_data.weather_infos["现在"].append(current_weather["humidity"])
            Weather_data.weather_infos["现在"].append(current_weather["feels"])
            Weather_data.weather_infos["今天"].append(other_day["_@STATE@_"]["forecast"][0]['lowTemp'])
            Weather_data.weather_infos["今天"].append(other_day["_@STATE@_"]["forecast"][0]['highTemp'])
            Weather_data.weather_infos["今天"].append(other_day["_@STATE@_"]["forecast"][0]['cap'])
            Weather_data.weather_infos["明天"].append(other_day["_@STATE@_"]["forecast"][1]['lowTemp'])
            Weather_data.weather_infos["明天"].append(other_day["_@STATE@_"]["forecast"][1]['highTemp'])
            Weather_data.weather_infos["明天"].append(other_day["_@STATE@_"]["forecast"][1]['cap'])
            Weather_data.weather_infos["后天"].append(other_day["_@STATE@_"]["forecast"][2]['lowTemp'])
            Weather_data.weather_infos["后天"].append(other_day["_@STATE@_"]["forecast"][2]['highTemp'])
            Weather_data.weather_infos["后天"].append(other_day["_@STATE@_"]["forecast"][2]['cap'])
            Weather_data.weather_infos["大后天"].append(other_day["_@STATE@_"]["forecast"][3]['lowTemp'])
            Weather_data.weather_infos["大后天"].append(other_day["_@STATE@_"]["forecast"][3]['highTemp'])
            Weather_data.weather_infos["大后天"].append(other_day["_@STATE@_"]["forecast"][3]['cap'])
        # 返回指定格式数据
        return Weather_data.weather_infos

    def get_local_data(self):
        Weather_data.weather_infos = {"城市": [], "现在": [], "今天": [], "明天": [], "后天": [], "大后天": []}
        r = requests.get(f'https://www.msn.cn/zh-cn/weather/forecast', headers=self.header)
        # 数据清洗
        self.soup = BeautifulSoup(r.text, 'html.parser')
        html_data = self.soup.findAll('script', type='application/json')
        # print(html_data)
        for data in html_data:
            json_data = json.loads(data.string)
            # print(json_data)
            current_weather = json_data["entryPoint"]["WeatherPageV2_default__WeatherPageV2"]["_@STATE@_"]["currentWeatherCondition"]
            other_day = json_data["WeatherData"]
            Weather_data.weather_infos["城市"] = other_day["_@STATE@_"]["currentLocation"]["displayName"]
            Weather_data.weather_infos["现在"].append(current_weather["shortCap"])
            Weather_data.weather_infos["现在"].append(current_weather["currentTemperature"])
            Weather_data.weather_infos["现在"].append(current_weather["aqiSeverity"])
            Weather_data.weather_infos["现在"].append(current_weather["humidity"])
            Weather_data.weather_infos["现在"].append(current_weather["feels"])
            Weather_data.weather_infos["今天"].append(other_day["_@STATE@_"]["forecast"][0]['lowTemp'])
            Weather_data.weather_infos["今天"].append(other_day["_@STATE@_"]["forecast"][0]['highTemp'])
            Weather_data.weather_infos["今天"].append(other_day["_@STATE@_"]["forecast"][0]['cap'])
            Weather_data.weather_infos["明天"].append(other_day["_@STATE@_"]["forecast"][1]['lowTemp'])
            Weather_data.weather_infos["明天"].append(other_day["_@STATE@_"]["forecast"][1]['highTemp'])
            Weather_data.weather_infos["明天"].append(other_day["_@STATE@_"]["forecast"][1]['cap'])
            Weather_data.weather_infos["后天"].append(other_day["_@STATE@_"]["forecast"][2]['lowTemp'])
            Weather_data.weather_infos["后天"].append(other_day["_@STATE@_"]["forecast"][2]['highTemp'])
            Weather_data.weather_infos["后天"].append(other_day["_@STATE@_"]["forecast"][2]['cap'])
            Weather_data.weather_infos["大后天"].append(other_day["_@STATE@_"]["forecast"][3]['lowTemp'])
            Weather_data.weather_infos["大后天"].append(other_day["_@STATE@_"]["forecast"][3]['highTemp'])
            Weather_data.weather_infos["大后天"].append(other_day["_@STATE@_"]["forecast"][3]['cap'])
        # 返回指定格式数据
        return Weather_data.weather_infos

    def get_cities_name(self):
        city_url = "https://area.site/china"
        soup = BeautifulSoup(requests.get(city_url).text, 'html.parser')
        div_oneBox_list = soup.find_all('div', class_='oneBox')
        # 提取所需数据
        data_list = []
        for div_oneBox in div_oneBox_list:
            data = {
                'class': div_oneBox['class'][0],
                'content': div_oneBox.text.strip()
            }
            data_list.append(data)
        # 将数据转换为json格式
        json_data = eval(json.dumps(data_list, ensure_ascii=False))
        cities = []
        for i in range(len(json_data) - 1):
            cities.append(json_data[i]["content"].split("\n\n"))
            # print(json_data[i]["content"].split("\n\n"))
        return cities


if __name__ == '__main__':
    weather = Weather_data()
    # print(weather.get_cities_name())
    # print(weather.get_data())
    # print(weather.get_local_data())
